"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Watcher = void 0;
/**
 * 观战者
 */
class Watcher {
    constructor(manual, user) {
        this.index = 0; //这个观战者读取棋谱内容的索引
        this.manual = manual;
        this.user = user;
        this.username = user.username;
        this.connection = user.connection;
        user.watcher = this;
        this.manual.addWatcher(this);
        this.startTimer();
    }
    gc() {
        this.manual.watchers.delete(this);
        this.manual = null;
        this.user = null;
        this.connection = null;
        clearInterval(this.timer);
    }
    startTimer() {
        this.timer = setInterval(() => {
            if (this.index == this.manual.list.length - 1) {
                clearInterval(this.timer);
                return;
            }
            this.sendMessage();
        }, 200);
    }
    update() {
        if (this.index == this.manual.list.length - 2) {
            this.sendMessage();
        }
    }
    sendMessage() {
        try {
            let json = this.manual.list[this.index++];
            this.connection.send(json);
        }
        catch (error) {
            console.error("观战者发送消息失败");
        }
    }
}
exports.Watcher = Watcher;
